<template>
  <e-table
    ref="eTable"
    :tableData="deptData"
    :columns="deptColumns"
    :loading="loading"
    :pageObj="null"
  >
    <el-table-column align="center" label="方式" slot="reportType">
      <template slot-scope="scope">
        <el-radio-group
          v-model="scope.row.notifierMode"
          @change="e => handleModeChange(e, scope.row)"
          size="small"
        >
          <el-radio-button :label="1">部门</el-radio-button>
          <el-radio-button :label="2">岗位</el-radio-button>
          <el-radio-button :label="3">人员</el-radio-button>
          <el-radio-button :label="4">角色</el-radio-button>
        </el-radio-group>
      </template>
    </el-table-column>
    <el-table-column label="通知部门" slot="noticeDept" align="center">
      <template slot-scope="scope">
        <template v-if="scope.row.notifierMode">
          <dept-select
            ref="deptSelect"
            :selectValue="scope.row.notifierDeptId"
            v-model="scope.row.dataDept"
            @select="e => handleDeptSelect(e, scope.row, 'dept')"
          />
        </template>
        <template v-else>请先选择方式</template>
      </template>
    </el-table-column>
    <el-table-column label="通知对象" slot="noticePosition" align="center">
      <template slot-scope="scope">
        <template v-if="scope.row.notifierMode === 1">该部门下所有人</template>
        <template v-else>
          <template v-if="scope.row.notifierMode === 2">
            <el-select
              v-model="scope.row.notifierPositionId"
              @select="e => handleDeptSelect(e, scope.row, 'position')"
            >
              <el-option
                v-for="(item,index ) in positionData"
                :key="index"
                :value="item.positionId"
                :label="item.positionName"
              />
            </el-select>
          </template>
          <!-- 人员列表 -->
          <template v-if="scope.row.notifierMode === 3">
            <el-select
              v-model="scope.row.notifierUserId"
              @select="e => handleDeptSelect(e, scope.row, 'user')"
            >
              <el-option
                v-for="(item,index ) in userData"
                :key="index"
                :value="item.userId"
                :label="item.name"
              />
            </el-select>
          </template>
          <!-- 角色列表 -->
          <template v-if="scope.row.notifierMode === 4">
            <el-select
              v-model="scope.row.notifierRoleId"
              @select="e => handleDeptSelect(e, scope.row, 'role')"
            >
              <el-option
                v-for="(item,index ) in roleData"
                :key="index"
                :value="item.roleId"
                :label="item.roleName"
              />
            </el-select>
          </template>
        </template>
      </template>
    </el-table-column>
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
// 通知人表格组件和上报表格公用

import DeptSelect from '@/components/DeptSelect'
import DeptPositionS from '@/components/DeptPositionS'
import { openNotification } from '@/utils/util'
import { queryPositionByDeptId } from '@/api/department'
import { queryUserByDeptId } from '@/api/assets'
import { getRoleList } from '@/api/role'

export default {
  components: {
    DeptSelect,
    DeptPositionS
  },
  props: {
    tableData: {
      type: Array,
      default: []
    },
    type: {
      type: String,
      default: ''
    }
  },
  watch: {
    tableData: {
      handler(n, o) {
        this.deptData = [...n]
        this.deptData.map(async v => {
          if (v.notifierMode === 2) {
            const res = await queryPositionByDeptId({ deptId: v.notifierDeptId })
            if (res.state === '000000') {
              this.positionData = res.data
            }
          } else if (v.notifierMode === 3) {
            //人员
            const res = await queryUserByDeptId({ deptId: v.notifierDeptId })
            if (res.state === '000000') {
              this.userData = res.data
            }
          }
        })
      }
    }
  },
  data() {
    return {
      loading: false,
      deptData: [],
      deptColumns: [{ slot: 'reportType' }, { slot: 'noticeDept' }, { slot: 'noticePosition' }, { slot: 'action' }],
      positionData: [], // 岗位数组
      userData: [],
      roleData: []
    }
  },
  methods: {
    handleModeChange(e, row) {
      const index = this.deptData.findIndex(v => v.xmId === row.xmId)
      this.deptData[index].notifierDeptId = null
      if (e === 1) {
        this.deptData[index].notifierPositionId = null
        this.deptData[index].notifierUserId = null
        this.deptData[index].notifierRoleId = null
      } else if (e === 2) {
        this.deptData[index].notifierUserId = null
        this.deptData[index].notifierRoleId = null
      } else if (e === 3) {
        this.deptData[index].notifierPositionId = null
        this.deptData[index].notifierRoleId = null
      } else {
        this.deptData[index].notifierPositionId = null
        this.deptData[index].notifierUserId = null
      }
    },
    async handleDeptSelect(value, row, type) {
      console.log('value', value, row)
      const index = this.deptData.findIndex(v => v.xmId === row.xmId)
      if (type === 'dept') {
        // 预警上报
        // 部门时，调用接口获取部门下的岗位
        if (row.notifierMode === 2) {
          const res = await queryPositionByDeptId({ deptId: value.id })
          if (res.state === '000000') {
            this.positionData = res.data
          }
        } else if (row.notifierMode === 3) {
          //人员
          const res = await queryUserByDeptId({ deptId: value.id })
          if (res.state === '000000') {
            this.userData = res.data
          }
        }
        this.$set(this.deptData[index], 'notifierDeptId', value.id)
      }
      //   else {
      //     this.$set(this.deptData[index], 'notifierPositionId', value.id)
      //   }
      this.$emit('tableUpdate', this.deptData)
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const index = _this.deptData.findIndex(v => v.xmId === row.xmId)
          _this.deptData.splice(index, 1)
          openNotification('success', '删除成功')
          _this.$emit('tableUpdate', _this.deptData)
        }
      })
    }
  },
  async created() {
    this.deptData = [...this.tableData]

    const res = await getRoleList()
    if (res.state === '000000') {
      this.roleData = res.data
    }
  }
}
</script>

<style scoped>
.data-table >>> .cell,
.data-table >>> .el-table,
.data-table >>> .el-table__body-wrapper {
  overflow: inherit;
}
</style>
